/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package ui.messages;

import constants.UiErrorConstants;
import exceptions.*;

import javax.swing.*;
import java.sql.SQLException;

/**
 * Contains all JOptionPane message boxes displayed to user during runtime
 *
 * @author rymek
 *         User: rymek
 *         Date: 20.07.11
 *         Time: 12:43
 */
public class MessageBoxes {

    /**
     * MessageBox displayed when SqlException is thrown
     *
     * @param err SQLException to display
     */
    public static void sqlException(SQLException err) {

        JOptionPane.showMessageDialog(null,
                                      err.getMessage(),
                                      UiErrorConstants.DESCR_ERR_DB,
                                      JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displayed when NoServerException is thrown
     * meaning, when user did not select server from
     * dropdown and is trying to connect to one.
     *
     * @param err NoServerException to display
     */
    public static void noServerException(NoServerException err) {

        JOptionPane.showMessageDialog(null,
                                      err.getMessage(),
                                      UiErrorConstants.DESCR_ERR_NO_SRV_SELECTED,
                                      JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displayed when NoMessageException is thrown
     * meaning, when user did not select message
     * from dropdown and is trying to perform search
     *
     * @param err NoMessageException to display
     */
    public static void noMessageException(NoMessageException err) {

        JOptionPane.showMessageDialog(null,
                                      err.getMessage(),
                                      UiErrorConstants.DESCR_ERR_NO_MSG_SELECTED,
                                      JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displayed when NoMessageType exception is thrown
     * meaning, when user did not select message type
     * from checkboxes. At least one message type must
     * be selected - request or response
     *
     * @param err NoMessageTypeException to display
     */
    public static void noMessageTypeException(NoMessageTypeException err) {

        JOptionPane.showMessageDialog(null,
                                      err.getMessage(),
                                      UiErrorConstants.ERR_NO_MESSAGE_TYPE_SELECTED,
                                      JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displayed when search did not return any results
     *
     * @param err NoResultsException to display
     */
    public static void noResultsFoundException(NoResultsException err) {

        JOptionPane.showMessageDialog(null,
                                      err.getMessage(),
                                      UiErrorConstants.DESCR_ERR_NO_RESULTS,
                                      JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     * Displayed when NoTnsAdminFIleException is thrown
     * meaning when user doesn't have specific system
     * variables set and TNS_NAMES.ora can't be found
     *
     * @param err NoTnsAdminFileException to display
     */
    public static void noTnsAdminException(NoTnsAdminFileException err) {

        JOptionPane.showMessageDialog(null,
                                      err.getMessage(),
                                      UiErrorConstants.DESCR_ERR_TNS_FILE,
                                      JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displays generic Exception message
     *
     * @param err Exception to display
     */
    public static void genericErrorMessage(Exception err) {

        JOptionPane.showMessageDialog(null,
                                      err.getMessage(),
                                      UiErrorConstants.DESCR_ERR_GENERIC,
                                      JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displays dialog to select oracle installation
     * directory for GetTNS class
     *
     * @return Oracle Client install path
     */
    public static String chooseOracleInstallDir() {

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        int returnVal = fileChooser.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return fileChooser.getSelectedFile().getAbsolutePath();
        }
        return null;
    }

    /**
     * Displays error when loading properties failed
     */
    public static void settingsLoadError() {

        JOptionPane.showMessageDialog(null,
                                      UiErrorConstants.ERR_NO_SETTINGS,
                                      UiErrorConstants.DESCR_ERR_NO_SETTINGS,
                                      JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displays error when loading result file failed
     */
    public static void resultLoadError() {

        JOptionPane.showMessageDialog(null,
                                      UiErrorConstants.ERR_NO_RESULT_FILE,
                                      UiErrorConstants.DESCR_ERR_NO_RESULT_FILE,
                                      JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displayed when no matching string is found withing
     * message body
     *
     * @param err NoMatchFoundException to display
     */
    public static void noMatchingStringFound(NoMatchFoundException err) {

        JOptionPane.showMessageDialog(null,
                                      err.getMessage(),
                                      UiErrorConstants.DESCR_ERR_NO_MATCH_FOUND,
                                      JOptionPane.ERROR_MESSAGE);
    }
}
